import axios from 'axios';
import qs from 'query-string';

export interface GoodsCatItem {
  id: number;
  name: string;
  fid: number;
  sort: number;
  status: number;
  image?: string;
  created_at: number;
  updated_at: number;
  children?: GoodsCatItem[];
  hasChildren?: boolean;
}

export interface GoodsCatListResponse {
  code: number;
  msg: string;
  data: {
    list: GoodsCatItem[];
    total: number;
  };
}

export interface CreateGoodsCatRequest {
  name: string;
  parent_id?: number;
  sort?: number;
  image?: string;
}

export interface UpdateGoodsCatRequest {
  id: number;
  name: string;
  parent_id?: number;
  sort?: number;
  image?: string;
}


export interface ApiResponse<T> {
  code: number;
  msg: string;
  data: T;
}

/**
 * 获取商品分类列表
 */
export function getGoodsCatList() {
  return axios.get<GoodsCatListResponse>('/admin/goods-cat', {
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
}

/**
 * 添加商品分类
 */
export function createGoodsCat(data: CreateGoodsCatRequest) {
  return axios.post<ApiResponse<GoodsCatItem>>('/admin/goods-cat/store', data);
}

/**
 * 更新商品分类
 */
export function updateGoodsCat(data: UpdateGoodsCatRequest) {
  return axios.post<ApiResponse<GoodsCatItem>>('/admin/goods-cat/update', data);
}

/**
 * 删除商品分类
 */
export function deleteGoodsCat(id: number) {
  return axios.post('/admin/goods-cat/del', { id });
} 